/*********************************************************************
 *
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2008, 2013, Willow Garage, Inc.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of Willow Garage, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *
 * Author: Eitan Marder-Eppstein
 *         David V. Lu!!
 *********************************************************************/
#ifndef COSTMAP_2D_STATIC_LAYER_H_
#define COSTMAP_2D_STATIC_LAYER_H_

#include <ros/ros.h>
#include <costmap_2d/costmap_layer.h>
#include <costmap_2d/layered_costmap.h>
#include <costmap_2d/GenericPluginConfig.h>
#include <dynamic_reconfigure/server.h>
#include <nav_msgs/OccupancyGrid.h>
#include <map_msgs/OccupancyGridUpdate.h>
#include <message_filters/subscriber.h>

namespace costmap_2d
{
// static_layer作为一个地图层，具备基本的更新边界和更新代价的功能，同时肩负着存储第一个静态地图 ( 处理建好的静态地图)。
class StaticLayer : public CostmapLayer
{
public:
    StaticLayer();

    virtual ~StaticLayer();
    // Costmap2DROS的构造函数会调用各Layer中的initialize函数，而initialize函数会调用onInitialize函数，真正的初始化工作在这里完成。
    virtual void onInitialize();

    virtual void activate();

    virtual void deactivate();

    virtual void reset();
    // 更新静态地图边界 若非rolling地图，在有地图数据更新时更新边界，否则，根据静态层更新的区域的边界更新传入的边界。
    virtual void updateBounds(double robot_x, double robot_y, double robot_yaw, double *min_x, double *min_y,
                              double *max_x, double *max_y);

    /**
     * @brief  更新静态地图代价
     * 若不是rolling地图，那么直接将静态地图层bound范围内的内容合并到主地图
     * 若为rolling地图，则找到静态地图和global系之间的坐标转换，通过主地图→global→静态地图的转换过程，找到主地图的cell在静态地图上对应的cost，赋值给主地图。
     * @param master_grid： master costmap 地图
     * @param min_i 地图最小i，以像素坐标表示
     * @param min_j 地图最小j，以像素坐标表示
     * @param max_i 地图最大i，以像素坐标表示
     * @param max_j 地图最大j，以像素坐标表示
     */
    virtual void updateCosts(costmap_2d::Costmap2D &master_grid, int min_i, int min_j, int max_i, int max_j);

    virtual void matchSize();

private:
    /** 将map放入到代价地图中
     * @brief  Callback to update the costmap's map from the map_server
     * @param new_map The map to put into the costmap.
     * The origin of the new map along with its size will determine what parts of the costmap's static map are overwritten.
     */
    void incomingMap(const nav_msgs::OccupancyGridConstPtr &new_map);

    void incomingUpdate(const map_msgs::OccupancyGridUpdateConstPtr &update);

    void reconfigureCB(costmap_2d::GenericPluginConfig &config, uint32_t level);

    /** 将接收到的地图“翻译”到本层地图
     * 接收到的地图上：unknown_cost_value（默认为-1）为未知区域，lethal_cost_threshold（默认100）以上为致命障碍物
     * 当接收到的地图上为-1时，若追踪未知区域，则本层地图上赋值NO_INFORMATION（255）；否则，在本层地图上赋值FREE_SPACE（0）；
     * 当接收到的地图上>=100时，在本层地图上赋值LETHAL_OBSTACLE（254）；
     * 若以上都不是，则按比例返回代价值。
     */
    unsigned char interpretValue(unsigned char value);

    std::string global_frame_;  ///< @brief The global frame for the costmap
    std::string map_frame_;  /// @brief frame that map is located in
    bool subscribe_to_updates_;
    bool map_received_;
    bool has_updated_data_;
    // 在接收到地图时，x_, y_被置为0,width_, height_ 被设为地图宽高
    unsigned int x_, y_, width_, height_;
    // 默认没有配置，使用true
    bool track_unknown_space_;
    bool use_maximum_;
    bool first_map_only_;      ///< @brief Store the first static map and reuse it on reinitializing
    bool trinary_costmap_;
    ros::Subscriber map_sub_, map_update_sub_;
    // lethal_threshold_：致命cost的阈值，使用默认的为100
    // unknown_cost_value_：未知的区域，cost 值为255
    unsigned char lethal_threshold_, unknown_cost_value_;
    dynamic_reconfigure::Server<costmap_2d::GenericPluginConfig> *dsrv_;
};

}  // namespace costmap_2d

#endif  // COSTMAP_2D_STATIC_LAYER_H_
